//
//  CustomMeasureView.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/12.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit
import pop
import Masonry

typealias CustomMeasureViewButtonTapsAction = ((Int, UIView) -> Void)

@objcMembers
class CustomMeasureView: UIView {

    var buttonTapsAction: CustomMeasureViewButtonTapsAction? {
        didSet {
            itemsView.buttonTapsAction = buttonTapsAction
        }
    }

    var isEnabled = false {
        didSet {
            if let itemViews = itemsView.subviews as? [CustomMeasureItemContainerView.ItemView] {
                itemViews.forEach({ (item) in
                    item.isEnabled = isEnabled
                })
            }
        }
    }

    let imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.image = #imageLiteral(resourceName: "mto_temp")
//        imageView.backgroundColor = .red
        return imageView
    }()

    lazy var itemsView: CustomMeasureItemContainerView = {
        let view = CustomMeasureItemContainerView()
        return view
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubview(imageView)
        addSubview(itemsView)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        imageView.mas_remakeConstraints { (make) in
            layoutViews({
                make?.edges.equalTo()(self)?.insets()(UIEdgeInsetsMake(43, 148, 171, 56))
            }) {
                make?.edges.equalTo()(self)?.insets()(UIEdgeInsetsMake(43, 148, 171, 56))
            }
        }
        itemsView.mas_remakeConstraints { (make) in
            layoutViews({
                make?.centerX.equalTo()(self.imageView)
                make?.bottom.equalTo()(self)?.offset()(-65)
            }, {
                make?.left.equalTo()(self.imageView.mas_centerX)
                make?.bottom.equalTo()(self)?.offset()(-65)
            })
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class CustomMeasureItemContainerView: UIView {

    var buttonTapsAction: CustomMeasureViewButtonTapsAction?

    override init(frame: CGRect) {
        super.init(frame: frame)
        let images = [#imageLiteral(resourceName: "custom_measure_item1"), #imageLiteral(resourceName: "custom_measure_item2"), #imageLiteral(resourceName: "custom_measure_item3")]

        for i in 0...2 {
            let itemView = ItemView()
            itemView.button.tag = i
            addSubview(itemView)
            itemView.button.setImage(images[i], for: .normal)
            itemView.button.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)

            itemView.mas_makeConstraints({ (make) in
                make?.size.equalTo()(CGSize(width: 110, height: 110))
                make?.top.bottom().equalTo()(self)
                make?.left.equalTo()(self)?.offset()(CGFloat(i) * (110 + 30))
                if i == 2 {
                    make?.right.equalTo()(self)
                }
            })
        }
    }

    @objc private func buttonClicked(_ sender: UIButton) {
        buttonTapsAction?(sender.tag, subviews[sender.tag])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

 extension CustomMeasureItemContainerView {

    class ItemView: UIView {

        var isEnabled: Bool = false {
            didSet {
                button.isEnabled = isEnabled
                if isEnabled {
                    addRotateAnimation(inView: largeCycleImageView, toValue: .pi * 2, duration: 2)
                    addRotateAnimation(inView: smallCycleImageView, toValue: .pi * 2)
                } else {
                    largeCycleImageView.pop_removeAllAnimations()
                    smallCycleImageView.pop_removeAllAnimations()
                }
            }
        }

        func addRotateAnimation(inView view: UIImageView, toValue: CGFloat, duration: CFTimeInterval = 3) {
            guard let anim = POPBasicAnimation(propertyNamed: kPOPLayerRotation) else { return }
            anim.repeatForever = true
            anim.duration = duration
            anim.timingFunction = CAMediaTimingFunction(name: kCAAnimationLinear)
            anim.toValue = toValue
            view.layer.pop_add(anim, forKey: nil)
        }

        let largeCycleImageView: UIImageView = {
            let imageView = UIImageView()
            imageView.image = #imageLiteral(resourceName: "custom_large_cycle")
            return imageView
        }()

        let smallCycleImageView: UIImageView = {
            let imageView = UIImageView()
            imageView.image = #imageLiteral(resourceName: "custom_small_cycle")
            return imageView
        }()

        let button = UIButton()

        override init(frame: CGRect) {
            super.init(frame: frame)

            button.isEnabled = false

            addSubview(largeCycleImageView)
            addSubview(smallCycleImageView)
            addSubview(button)

            largeCycleImageView.mas_makeConstraints { (make) in
                make?.edges.equalTo()(self);
            }
            smallCycleImageView.mas_makeConstraints { (make) in
                make?.edges.equalTo()(self);
            }
            button.mas_makeConstraints { (make) in
                make?.edges.equalTo()(self);
            }
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
}
